import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class file_2445 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的目录：");
        String rootPath = scanner.next();
        if (rootPath == null || rootPath.equals("")) {
            System.out.println("请注意：目录不能为空!!!");
            return;
        }
        File rootDir = new File(rootPath);
        if (rootDir.isDirectory() == false) {
            System.out.println("当前输入的不是一个目录！");
            return;
        }
        System.out.println("请输入要找出文件名中所含的字符串：");
        String token = scanner.next();
        List<File> fileList = new ArrayList<>();
        scanDir(rootDir, token, fileList);
        if (fileList.size() == 0) {
            System.out.println("当前没有找到符合条件的文件");
            return;
        }
        for (File file : fileList) {
            System.out.println("请问您要删除文件" + file.getAbsolutePath() + "吗？(y/n)");
            String order = scanner.next();
            if (order.equals("y")) {
                file.delete();
            }
        }
    }
    private static void scanDir(File rootDir, String token, List<File> fileList) {
        File[] files = rootDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File file:files) {
            if (file.isDirectory()) {
                scanDir(file, token, fileList);
            } else {
                System.out.println(token);
                if (file.getName().contains(token)) {
                    fileList.add(file.getAbsoluteFile());
                }
            }
        }
    }
}